package ar.com.solidussource.howmuchmobile.activities.maps;

import android.os.Bundle;
import ar.com.solidussource.howmuchmobile.R;
import ar.com.solidussource.howmuchmobile.common.entities.Geopoint;
import ar.com.solidussource.howmuchmobile.common.entities.Market;
import ar.com.solidussource.howmuchmobile.common.utils.ClusterService;
import ar.com.solidussource.howmuchmobile.common.utils.MarketCluster;
import ar.com.solidussource.howmuchmobile.maps.items.MarketClusterItem;
import ar.com.solidussource.howmuchmobile.maps.items.MarketItem;
import ar.com.solidussource.howmuchmobile.maps.overlays.MarketClusterItemizedOverlay;
import ar.com.solidussource.howmuchmobile.maps.overlays.MarketItemizedOverlay;
import ar.com.solidussource.howmuchmobile.maps.tools.MapViewWithChangeListener;
import ar.com.solidussource.howmuchmobile.services.MarketRestService;
import ar.com.solidussource.howmuchmobile.services.mocks.MarketGlobalPositioningService;
import com.google.android.maps.*;

import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: FT31270775
 * Date: 12/11/12
 * Time: 16:13
 * To change this template use File | Settings | File Templates.
 */
public class HowMuchMapActivity extends MapActivity {
    private int lastZoomLevel = 17;
    private List<Market> markets;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        try{
            //Obtenemos una referencia al control MapView
            final MapViewWithChangeListener mapa = (MapViewWithChangeListener) findViewById(R.id.mapa);

            MarketRestService marketRestService = new MarketRestService();
            markets = marketRestService.getMarkets(1, 100);
            //MarketGlobalPositioningService service = new MarketGlobalPositioningService();
            //markets = service.getMarkets();

            final MapController controlMapa = mapa.getController();

            //Center MAP
            Double latitud = -34.633*1E6;
            Double longitud = -58.791*1E6;
            GeoPoint loc = new GeoPoint(latitud.intValue(), longitud.intValue());
            controlMapa.setCenter(loc);

            //Zoom Controller
            mapa.setBuiltInZoomControls(true);
            controlMapa.setZoom(lastZoomLevel);

            mapa.setOnChangeListener(new MapViewChangeListener(this, markets));

            processOverLays(mapa, markets);

        } catch (Exception e){

        }

    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    public synchronized void processOverLays(MapView mapa, List<Market> markets){
        //drawing markets

        ClusterService clusterService = new ClusterService();
        List<MarketCluster> marketClusters = clusterService.generateClusters(mapa, markets);

        List<Overlay> mapOverlays = mapa.getOverlays();
        mapOverlays.clear();

        MarketClusterItemizedOverlay marketClusterItemizedOverlay = new MarketClusterItemizedOverlay(this.getResources().getDrawable(R.drawable.cluster1), this);
        MarketItemizedOverlay marketItemizedOverlay = new MarketItemizedOverlay(this.getResources().getDrawable(R.drawable.cart3), this);

        Iterator<MarketCluster> ite = marketClusters.iterator();
        while(ite.hasNext()){
            MarketCluster marketCluster = ite.next();

            Geopoint geopoint = clusterService.getCluterGlobalPositioning(marketCluster);
            Double lat = new Double(geopoint.getLatitude());
            Double lon = new Double(geopoint.getLongitude());

            GeoPoint geoPoint = new GeoPoint(lat.intValue(), lon.intValue());

            if ( marketCluster.getMarkets().size() > 1 ) {
                MarketClusterItem marketClusterItem = new MarketClusterItem(marketCluster, geoPoint, "cluster", Integer.toString(marketCluster.getMarkets().size()));
                marketClusterItemizedOverlay.addOverlay(marketClusterItem);
            }
            else {
                if( marketCluster.getMarkets().size() > 0 ){
                    MarketItem marketItem = new MarketItem(marketCluster.getMarkets().get(0), geoPoint, marketCluster.getMarkets().get(0).getMarketcategory().getName(), marketCluster.getMarkets().get(0).getName());
                    marketItemizedOverlay.addOverlay(marketItem);
                }
            }
        }

        if( marketClusterItemizedOverlay.size() > 0 ) mapOverlays.add(marketClusterItemizedOverlay);
        if( marketItemizedOverlay.size() > 0 ) mapOverlays.add(marketItemizedOverlay);

        mapOverlays.add(marketItemizedOverlay);

    }

}

class MapViewChangeListener implements MapViewWithChangeListener.OnChangeListener {

    private HowMuchMapActivity mContext;
    private List<Market> mMarkets;
    private GeoPoint lastCenter = null;

    public MapViewChangeListener(HowMuchMapActivity context, List<Market> markets) {
        super();
        mContext = context;
        mMarkets = markets;
    }

    public void onChange(MapView view, GeoPoint newCenter, GeoPoint oldCenter,
                         int newZoom, int oldZoom) {

        final MapController controlMapa = view.getController();
        controlMapa.setZoom(newZoom);
        mContext.processOverLays(view, mMarkets);

    }
}
